package com.example.administrator.kotiln.ui;

import android.app.Activity;
import android.content.res.ColorStateList;
import android.content.res.Resources;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.view.ViewPager;
import android.util.TypedValue;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.kotiln.R;
import com.example.administrator.kotiln.adapter.MyPagerAdapters;
import com.example.administrator.kotiln.base.BaseActivity;
import com.example.administrator.kotiln.utils.Share;
import com.example.administrator.kotiln.utils.Utility;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/10/16.
 */

public class ViewPagersActivity extends BaseActivity {
    private ViewPager mPager;
    int position = 50000;
    private boolean isTouched = false;
    private List<View> mList;

    private Handler mHandler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            // TODO Auto-generated method stub
            super.handleMessage(msg);
            mPager.setCurrentItem(position);
        }

    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Utility.setActionBar(this);
        setContentView(R.layout.activity_viewpager);
        mPager = (ViewPager) findViewById(R.id.main_viewpager);

        //返回
        ImageView back = (ImageView) findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        //构造list集合
        List<Integer> list = new ArrayList<Integer>();
        list.add(R.mipmap.image1);
        list.add(R.mipmap.image2);
        list.add(R.mipmap.image3);
        list.add(R.mipmap.image4);


        mList = new ArrayList<View>();
        for (int i = 0; i < list.size(); i++) {
            RelativeLayout rootlayout = new RelativeLayout(this);
            rootlayout.setLayoutParams(new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
            rootlayout.setId(0);
            ImageView imageView = new ImageView(this);
            RelativeLayout.LayoutParams image_Params = new RelativeLayout.LayoutParams(
                    dip2px(250), dip2px(192));
            image_Params.setMargins(dip2px(5), 0, dip2px(5), dip2px(5));
            imageView.setLayoutParams(image_Params);
            imageView.setImageResource(list.get(i));
            imageView.setId(1);

            //字体颜色
            Resources resource = (Resources) getBaseContext().getResources();
            ColorStateList csl = (ColorStateList) resource.getColorStateList(R.color.black);


            TextView text_Name = new TextView(this);
            RelativeLayout.LayoutParams text_Name_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_Name_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
            text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
            text_Name_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_Name.setLayoutParams(text_Name_Params);
            text_Name.setText("图书描述：");
            text_Name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            text_Name.setTextColor(csl);
            text_Name.setId(2);

            TextView text_Email = new TextView(this);
            RelativeLayout.LayoutParams text_Email_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_Email_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
            text_Email_Params.addRule(RelativeLayout.BELOW, text_Name.getId());
            text_Email_Params.setMargins(dip2px(10), dip2px(5), dip2px(10), 0);
            text_Email.setLayoutParams(text_Email_Params);
            text_Email.setText("《诗经》是中国古代诗歌开端，最早的一部诗歌总集，收集了西周初年至春秋中叶（前11世纪至前6世纪）的诗歌，共311篇，其中6篇为笙诗，即只有标题，没有内容，称为笙诗六篇");
            text_Email.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            text_Email.setId(3);


            TextView text_author = new TextView(this);
            RelativeLayout.LayoutParams text_author_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_author_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
            text_author_Params.addRule(RelativeLayout.BELOW, text_Email.getId());
            text_author_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_author.setLayoutParams(text_author_Params);
            text_author.setText("图书作者：小李");
            text_author.setTextColor(csl);
            text_author.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            text_author.setId(4);


            TextView text_place = new TextView(this);
            RelativeLayout.LayoutParams text_place_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_place_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
            text_place_Params.addRule(RelativeLayout.BELOW, text_author.getId());
            text_place_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_place.setLayoutParams(text_place_Params);
            text_place.setText("出版社：北京");
            text_place.setTextColor(csl);
            text_place.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            text_place.setId(5);


            TextView text_data = new TextView(this);
            RelativeLayout.LayoutParams text_data_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_data_Params.addRule(RelativeLayout.RIGHT_OF, imageView.getId());
            text_data_Params.addRule(RelativeLayout.BELOW, text_place.getId());
            text_data_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_data.setLayoutParams(text_data_Params);
            text_data.setText("出版日期：2015-6-18");
            text_data.setTextColor(csl);
            text_data.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            text_data.setId(6);


            TextView text_description_name = new TextView(this);
            RelativeLayout.LayoutParams text_DescriptionName_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_DescriptionName_Params.addRule(RelativeLayout.BELOW, imageView.getId());
            //text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
            text_DescriptionName_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_description_name.setLayoutParams(text_DescriptionName_Params);
            text_description_name.setText("图书简介：");
            text_description_name.setTextColor(csl);
            text_description_name.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
            text_description_name.setId(7);


            TextView text_description = new TextView(this);
            RelativeLayout.LayoutParams text_Description_Params = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
            text_Description_Params.addRule(RelativeLayout.BELOW, text_description_name.getId());
            //text_Name_Params.addRule(RelativeLayout.ALIGN_TOP, imageView.getId());
            text_Description_Params.setMargins(dip2px(10), dip2px(5), 0, 0);
            text_description.setLayoutParams(text_Description_Params);
            text_description.setText("11111111111111111111111111111111111111111111111111111111111111111111111111111");
            text_description.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
            text_description.setId(8);


            rootlayout.addView(imageView, image_Params);
            rootlayout.addView(text_Name, text_Name_Params);
            rootlayout.addView(text_Email, text_Email_Params);
            rootlayout.addView(text_description, text_Description_Params);
            rootlayout.addView(text_author, text_author_Params);
            rootlayout.addView(text_place, text_place_Params);
            rootlayout.addView(text_data, text_data_Params);
            rootlayout.addView(text_description_name, text_DescriptionName_Params);
            // setContentView(rootlayout);
            mList.add(rootlayout);
        }
        Share.d("mlist" + mList.size());

        MyPagerAdapters adapter = new MyPagerAdapters(this, mList);
        mPager.setAdapter(adapter);
        mPager.setCurrentItem(position);
        //实现思路三
        mPager.setOnTouchListener(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View arg0, MotionEvent event) {
                // TODO Auto-generated method stub
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                    case MotionEvent.ACTION_MOVE:
                        isTouched = true;
                        break;
                    case MotionEvent.ACTION_UP:
                        isTouched = false;
                    default:
                        break;
                }
                return false;
            }
        });
        mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                // TODO Auto-generated method stub
                position = arg0;
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });
        //实现思路二
        Thread change = new Thread(new Runnable() {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                while (true) {
                    if (!isTouched) {
                        position++;
                        mHandler.sendEmptyMessage(0);
                    }
                    try {
                        Thread.sleep(3000);
                    } catch (InterruptedException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        });
        change.start();
    }


    public int dip2px(float dpValue) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }
}
